﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint.Taxonomy;

namespace DR5.SPExtend
{
    public static class TaxonomyExtension
    {
        public static Term GetSubTerm(this Term term, string termStr)
        {
            //l.wl(termCollection.GetPath());
            if (term.Name == termStr)
            {
                //l.wl("matched123");
                return term;
            }

            foreach (Term t in term.Terms)
            {
                Term tt = GetSubTerm(t, termStr);
                if (tt != null)
                    return tt;
            }
            return null;
        }

        public static Term GetTerm(this TermCollection termCollection, string termStr)
        {
            foreach (Term t in termCollection)
            {
                if (t.Name == termStr)
                    return t;
                else
                {
                    Term tt = GetTerm(t.Terms, termStr);
                    if (tt != null)
                        return tt;
                }
            }
            return null;
        }

        public static string GetTermText(this TaxonomyWebTaggingControl tc)
        {
            string txt = tc.Text;
            txt = txt.Contains("|") ? txt.Split('|')[0] : txt;
            return txt;
        }

        public static string GetTermId(this TaxonomyWebTaggingControl tc)
        {
            string txt = tc.Text;
            txt = txt.Contains("|") ? txt.Split('|')[1] : txt;
            return txt;
        }

        //public static string GetTermText(this TaxonomyWebTaggingControl tc)
        //{
        //    TaxonomyFieldValueCollection values = new TaxonomyFieldValueCollection(String.Empty);
        //    values.PopulateFromLabelGuidPairs(tc.Text);
        //    return values[0].Label;
        //}

        public static TaxonomyFieldValue GetTaxonomyFieldValue(this TaxonomyWebTaggingControl tc)
        {
            TaxonomyFieldValue values = new TaxonomyFieldValue(String.Empty);
            values.PopulateFromLabelGuidPair(tc.Text);
            return values;
        }

        public static void SetTerm(this TaxonomyWebTaggingControl tc, Term term)
        {
            tc.Text = term.Name + "|" + term.Id;
            //tc.Text = "1033;#" + term.Name + "|" + term.Id;
        }

        /// <summary>
        /// Method not available
        /// </summary>
        /// <param name="tc"></param>
        /// <param name="termId"></param>
        public static void SetTerm(this TaxonomyWebTaggingControl tc, string termId)
        {

        }

        public static void SetTermSet(this TaxonomyWebTaggingControl tc, TermStore termStore, string groupName, string termsetName, bool isMulti)
        {
            Group group = termStore.Groups[groupName];
            TermSet termset = group.TermSets[termsetName];

            tc.SspId.Add(termStore.Id);
            tc.SSPList = termStore.Id.ToString();
            tc.TermSetId.Add(termset.Id);
            tc.TermSetList = termset.Id.ToString();
            tc.IsMulti = isMulti;
        }
    }
}
